<?php
namespace app\login\controller;

use app\login\model\User;
use think\Controller;
use think\Session;
use think\Request;
use app\login\validate\Dologin;
use think\captcha\Captcha;

class Login extends Controller{
    public function login(){
        $hint = Session::get('Hint');
        $this->assign('hint',$hint);
        return $this->fetch();
    }

    public function dologin(){
        if(Request::instance()->isPost()){
            $has = Request::instance()->post();
            //var_dump($has);
            $validate = new Dologin();
            if(!$validate->check($has)){
                $hint=$validate->getError();
                Session::set('Hint',$hint);
                $this->redirect('login/Login/login');
            }else{
                $UserModel = new User();
                $user = $UserModel->checkUser($has['Account'],$has['Password']);
                if(is_array($user)){
                    if($user['status']==0){
                        $hint = '该用户审核中';
                        Session::set('Hint',$hint);
                        $this->redirect('login/Login/login');
                    }else{
                        Session::set('Uname',$user['userName']);
                        Session::set('Ustatus',$user['userstatus']);
                        Session::set('Uid',$user['Id']);
                        $this->redirect('index/Index/index');
                    }
                }else{
                    $hint = $user;
                    Session::set('Hint',$hint);
                    $this->redirect('login/Login/login');
                }
            }
        }
    }

    public function captcha(){
        $config = [
            'codeSet' => '0123456789',
            'length' => 4,
            'useNoise' => false,
            ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }
}
?>